package {
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.net.URLLoader;
  import flash.net.URLRequest;
  import flash.utils.Timer;

  [SWF(width="1680", height="930", backgroundColor="#EBD6A5", frameRate="30")]

  public class basic_mode_viz extends Sprite
  {
    
    // CONSTANTS
    private var color_black:uint = 0x000000;
    private var color_gray:uint = 0x797979;
    private var color_white:uint = 0xffffff;
    
    private var myFrame:Frame;
    
    private var myComboBoxes:ComboBoxes;
    
    private var DataArray_black:Array;  // contains all 11 color sets as children
    private var DataArray_gray:Array;
    private var DataArray_white:Array;
    private var num_loaded:int = 0;
    
    private var EntryArray_black:Array;
    private var EntryArray_gray:Array;
    private var EntryArray_white:Array;
    
    private var ColorArray:Array;  // contains all the RGB values corresponding to chip_num starting from 0 to 453
    
    private var spriteContainer_black:Sprite;
    private var spriteContainer_gray:Sprite;
    private var spriteContainer_white:Sprite;
    
    private var highlightContainer:Sprite;
    private var tmr:Timer;
    private var highlightColor:uint = 0xffffff;    // change when background changes
    
    public function basic_mode_viz()
    {
      myFrame = new Frame(1680, 930, 0xd0d0d0, 0xc0c0c0);
      myFrame.drawFrame();
      addChild(myFrame);
      
      myComboBoxes = new ComboBoxes();
      addChild(myComboBoxes);
      myComboBoxes.addEventListener("bg display change", bgDisplayChangeHandler);
      myComboBoxes.addEventListener("bg select change", bgSelectChangeHandler);
      
      highlightContainer = new Sprite();
      tmr = new Timer(100, 999999);
		  tmr.addEventListener("timer", tmrHandler);
		  tmr.start();

      
      
      initEntryArrays();
      loadAllData();
      addChild(highlightContainer);
      
      this.addEventListener("load complete", onDataLoadComplete);
      
    }
    
    private function tmrHandler(event:Event):void{
      if(highlightContainer.visible == true)
        highlightContainer.visible = false;
      else
        highlightContainer.visible = true;
    }
    
    private function initEntryArrays():void{
      
      var num_color_containers:int = 11;
      
      EntryArray_black = new Array(num_color_containers);
      EntryArray_gray = new Array(num_color_containers);
      EntryArray_white = new Array(num_color_containers);
      
      for(var i:int = 0; i < num_color_containers; i++){
        EntryArray_black[i] = new Array();
        EntryArray_gray[i] = new Array();
        EntryArray_white[i] = new Array();
      }
    }
    
    private function loadAllData():void{
      // LOAD DATA
      var black_path:String = "../dat/basic_mode_black_viz.txt";
      var gray_path:String = "../dat/basic_mode_gray_viz.txt";
      var white_path:String = "../dat/basic_mode_white_viz.txt";
      var colors_path:String = "../dat/colors.txt";
      
      var black_urlRequest:URLRequest = new URLRequest(black_path);
      var gray_urlRequest:URLRequest = new URLRequest(gray_path);
      var white_urlRequest:URLRequest = new URLRequest(white_path);
      var colors_urlRequest:URLRequest = new URLRequest(colors_path);
      
      var black_urlLoader:URLLoader = new URLLoader();
      var gray_urlLoader:URLLoader = new URLLoader();
      var white_urlLoader:URLLoader = new URLLoader();
      var colors_urlLoader:URLLoader = new URLLoader();
     
     ColorArray = new Array();
      colors_urlLoader.load(colors_urlRequest);
      colors_urlLoader.addEventListener(Event.COMPLETE, colors_urlLoader_complete);
      function colors_urlLoader_complete(evt:Event):void {
        var ColorData:Array = colors_urlLoader.data.split("\n");
        
        var i:int = 0;
        for(i = 0; i < ColorData.length; i++){
          var splitColor:Array = ColorData[i].split(",");
          var red:int = parseInt(splitColor[0]);
          var green:int = parseInt(splitColor[1]);
          var blue:int = parseInt(splitColor[2]);
          
          var thisColor:ColorRGB = new ColorRGB(red, green, blue);
          ColorArray.push(thisColor);
        }
        dispatchEvent(new Event("load complete"));
      }
     
      black_urlLoader.load(black_urlRequest);
      black_urlLoader.addEventListener(Event.COMPLETE, black_urlLoader_complete);
      function black_urlLoader_complete(evt:Event):void {
        DataArray_black = black_urlLoader.data.split("\n");
        
        var cur_container:int = 0;
        var i:int = 0;
        while(i < DataArray_black.length - 1){
          
          var line:String = DataArray_black[i] as String;
          
          // do nothing for comments
          if(line.charAt(0) == "#"){
            i++;
            trace(line);
          }
          // if demarking a new container, update cur_container so we place entries in right container
          else if(line.substr(0, 9) == "container"){
            trace("new container: " + line);
            //trace(DataArray_black[i].substr(10, DataArray_black[i].length));
            var container_color:String = DataArray_black[i].substr(10, DataArray_black[i].length);
            cur_container = find_container_number(container_color);
            i++;
          }
          // new entry, most common case
          else if(is_number(line.charAt(0))){
            var arr:Array = line.split(";");
            var chip_num:int = parseInt(arr[0]);
            var count:int = parseInt(arr[1]);
            var num_neighbors:int = parseInt(arr[2]);
            trace("add entry: " + chip_num + " - " + count + " - " + num_neighbors);
            
            var ce:ColorEntry = new ColorEntry(chip_num, count);
            if(num_neighbors > 0){
              for(var j:int = 0; j < num_neighbors; j++){
                var arr_neighbor:Array = DataArray_black[i+j+1].split(";");
                ce.addNeighbor(arr_neighbor[0], parseInt(arr_neighbor[1]));
              }
            }
            EntryArray_black[cur_container].push(ce);
            i += num_neighbors + 1;
          }
          //trace(i);
        }
        trace('done');
        dispatchEvent(new Event("load complete"));
      }
      
      gray_urlLoader.load(gray_urlRequest);
      gray_urlLoader.addEventListener(Event.COMPLETE, gray_urlLoader_complete);
      function gray_urlLoader_complete(evt:Event):void {
        DataArray_gray = gray_urlLoader.data.split("\n");
        
        var cur_container:int = 0;
        var i:int = 0;
        while(i < DataArray_gray.length - 1){
          
          var line:String = DataArray_gray[i] as String;
          
          // do nothing for comments
          if(line.charAt(0) == "#"){
            i++;
            trace(line);
          }
          // if demarking a new container, update cur_container so we place entries in right container
          else if(line.substr(0, 9) == "container"){
            trace("new container: " + line);
            var container_color:String = DataArray_gray[i].substr(10, DataArray_gray[i].length);
            cur_container = find_container_number(container_color);
            i++;
          }
          // new entry, most common case
          else if(is_number(line.charAt(0))){
            var arr:Array = line.split(";");
            var chip_num:int = parseInt(arr[0]);
            var count:int = parseInt(arr[1]);
            var num_neighbors:int = parseInt(arr[2]);
            trace("add entry: " + chip_num + " - " + count + " - " + num_neighbors);
            
            var ce:ColorEntry = new ColorEntry(chip_num, count);
            if(num_neighbors > 0){
              for(var j:int = 0; j < num_neighbors; j++){
                var arr_neighbor:Array = DataArray_gray[i+j+1].split(";");
                ce.addNeighbor(arr_neighbor[0], parseInt(arr_neighbor[1]));
              }
            }
            EntryArray_gray[cur_container].push(ce);
            i += num_neighbors + 1;
          }
          //trace(i);
        }
        trace('done');
        dispatchEvent(new Event("load complete"));
      }
      
      white_urlLoader.load(white_urlRequest);
      white_urlLoader.addEventListener(Event.COMPLETE, white_urlLoader_complete);
      function white_urlLoader_complete(evt:Event):void {
        DataArray_white = white_urlLoader.data.split("\n");
        
        var cur_container:int = 0;
        var i:int = 0;
        while(i < DataArray_white.length - 1){
          
          var line:String = DataArray_white[i] as String;
          
          // do nothing for comments
          if(line.charAt(0) == "#"){
            i++;
            trace(line);
          }
          // if demarking a new container, update cur_container so we place entries in right container
          else if(line.substr(0, 9) == "container"){
            trace("new container: " + line);
            var container_color:String = DataArray_white[i].substr(10, DataArray_white[i].length);
            cur_container = find_container_number(container_color);
            i++;
          }
          // new entry, most common case
          else if(is_number(line.charAt(0))){
            var arr:Array = line.split(";");
            var chip_num:int = parseInt(arr[0]);
            var count:int = parseInt(arr[1]);
            var num_neighbors:int = parseInt(arr[2]);
            trace("add entry: " + chip_num + " - " + count + " - " + num_neighbors);
            
            var ce:ColorEntry = new ColorEntry(chip_num, count);
            if(num_neighbors > 0){
              for(var j:int = 0; j < num_neighbors; j++){
                var arr_neighbor:Array = DataArray_white[i+j+1].split(";");
                ce.addNeighbor(arr_neighbor[0], parseInt(arr_neighbor[1]));
              }
            }
            EntryArray_white[cur_container].push(ce);
            i += num_neighbors + 1;
          }
          //trace(i);
        }
        trace('done');
        dispatchEvent(new Event("load complete"));
      }
    }
    
    private function onDataLoadComplete(event:Event):void{
      num_loaded++;
      
      // data load is complete, check data here, and plot
      if(num_loaded == 4){
        trace('LOADING COMPLETE!');
        plotContainers();
      }
    }
    
    // IMPORTANT!  This function handles all 
    private function plotContainers():void{
      /* go through black, white, and gray backgrounds, giving each of its 11 children containers
      an ox and oy.  Go through the array, making a new sprite with dimension proportional to the count
      of the entry.  Keep track of the x-amount we've drawn, changing lines as necessary when x approaches
      the limit.  For each sprite have an event listener for mouse over that highlights using the 
      index and containers.
      
      Finally, hide the two non-selected backgrounds
      */
      
      spriteContainer_black = new Sprite();
      spriteContainer_gray = new Sprite();
      spriteContainer_white = new Sprite();
      
      spriteContainer_black.visible = true;
      spriteContainer_gray.visible = false;
      spriteContainer_white.visible = false;
      
      plotBlackContainer();
      plotGrayContainer();
      plotWhiteContainer();
      
      addChild(spriteContainer_black);
      addChild(spriteContainer_gray);
      addChild(spriteContainer_white);
    }
    
    private function plotBlackContainer():void{
      // step 1: plot the black container
      var ox:int = 10;
      var max_x:int = 1440;
      for(var i:int = 0; i < EntryArray_black.length; i++){
        var cur_x:int = ox;
        var container:Array = EntryArray_black[i] as Array;
        var oy:int = find_oy_from_container_index(i);
        var thisContainerSprite:Sprite = new Sprite();
        
        for(var j:int = 0; j < container.length; j++){
          var thisEntry:ColorEntry = container[j] as ColorEntry;
          var dim:int = find_dim_from_count(thisEntry.getCount());
          var chip_num:int = thisEntry.getChipNum();
          var colorrgb:ColorRGB = ColorArray[chip_num];
          var color:uint = (colorrgb.getRed()<<16) + (colorrgb.getGreen()<<8) + colorrgb.getBlue();

          var thisSprite:Sprite = new Sprite();
          thisSprite.x = cur_x;
          thisSprite.y = oy;
          thisSprite.name = i.toString() + ";" + j.toString();    // store the container index and the position index for event listeners
          
          thisSprite.graphics.beginFill(color);
          thisSprite.graphics.drawRect(0, 0, dim, dim);
          thisSprite.graphics.endFill();

          thisSprite.addEventListener(MouseEvent.MOUSE_OVER,
          function (event:MouseEvent):void
          {
            var target:Sprite = event.target as Sprite;
            //target.graphics.clear();
            var param_array:Array = target.name.split(";");
            var temp_container:Array = EntryArray_black[parseInt(param_array[0])] as Array;
            var tempEntry:ColorEntry = temp_container[parseInt(param_array[1])] as ColorEntry;
            var temp_chip_num:int = tempEntry.getChipNum();
            var temp_dim:int = find_dim_from_count(tempEntry.getCount());

            /*
            var colorrgb:ColorRGB = ColorArray[temp_chip_num];
            var color:uint = (colorrgb.getRed()<<16) + (colorrgb.getGreen()<<8) + colorrgb.getBlue();
            */
            var newhighlight:Sprite = new Sprite;
            newhighlight.x = target.x - 3;
            newhighlight.y = target.y - 3;
            newhighlight.graphics.lineStyle(2, highlightColor, 0.7);
            newhighlight.graphics.beginFill(0x000000, 0);
            newhighlight.graphics.drawRect(0, 0, temp_dim + 6, temp_dim + 6);
            newhighlight.graphics.endFill();
            highlightContainer.addChild(newhighlight);
            
            // NOW GO THROUGH ALL THE NEIGHBORS AND HIGHLIGHT THOSE TOO!
            // better yet, don't highlight them, just draw squares in a highlight array
            for(var k:int = 0; k < tempEntry.getNumNeighbors(); k++){
              var tempNeighbor:Neighbor = tempEntry.getNeighborAt(k);
              var cont_num:int = find_container_number(tempNeighbor.getColor());
              var index:int = tempNeighbor.getIndex();
              var tmp_container:Sprite = spriteContainer_black.getChildAt(cont_num) as Sprite;
              var neighbor_sprite:Sprite = tmp_container.getChildAt(index) as Sprite;
              
              var param_array2:Array = neighbor_sprite.name.split(";");
              var temp_container2:Array = EntryArray_black[parseInt(param_array2[0])] as Array;
              var tempEntry2:ColorEntry = temp_container2[parseInt(param_array2[1])] as ColorEntry;
              var temp_chip_num2:int = tempEntry2.getChipNum();
              var temp_dim2:int = find_dim_from_count(tempEntry2.getCount());
              
              var newhighlight2:Sprite = new Sprite;
              newhighlight2.x = neighbor_sprite.x - 3;
              newhighlight2.y = neighbor_sprite.y - 3;
              newhighlight2.graphics.lineStyle(2, highlightColor, 0.7);
              newhighlight2.graphics.beginFill(0x000000, 0);
              newhighlight2.graphics.drawRect(0, 0, temp_dim2 + 6, temp_dim2 + 6);
              newhighlight2.graphics.endFill();
              highlightContainer.addChild(newhighlight2);
            }
          });
          thisSprite.addEventListener(MouseEvent.MOUSE_OUT,
          function (event:MouseEvent):void
          {
            var target:Sprite = event.target as Sprite;
            while(highlightContainer.numChildren > 0)
              highlightContainer.removeChildAt(0);

          });
          
          cur_x += dim+5;
          if(cur_x > max_x){
            cur_x = ox;
            oy += 35;
          }
          thisContainerSprite.addChild(thisSprite);
        }
        spriteContainer_black.addChild(thisContainerSprite);
      }
    }
    
    private function plotGrayContainer():void{
      // step 1: plot the gray container
      var ox:int = 10;
      var max_x:int = 1440;
      for(var i:int = 0; i < EntryArray_gray.length; i++){
        var cur_x:int = ox;
        var container:Array = EntryArray_gray[i] as Array;
        var oy:int = find_oy_from_container_index(i);
        var thisContainerSprite:Sprite = new Sprite();
        
        for(var j:int = 0; j < container.length; j++){
          var thisEntry:ColorEntry = container[j] as ColorEntry;
          var dim:int = find_dim_from_count(thisEntry.getCount());
          var chip_num:int = thisEntry.getChipNum();
          var colorrgb:ColorRGB = ColorArray[chip_num];
          var color:uint = (colorrgb.getRed()<<16) + (colorrgb.getGreen()<<8) + colorrgb.getBlue();

          var thisSprite:Sprite = new Sprite();
          thisSprite.x = cur_x;
          thisSprite.y = oy;
          thisSprite.name = i.toString() + ";" + j.toString();    // store the container index and the position index for event listeners
          
          thisSprite.graphics.beginFill(color);
          thisSprite.graphics.drawRect(0, 0, dim, dim);
          thisSprite.graphics.endFill();

          thisSprite.addEventListener(MouseEvent.MOUSE_OVER,
          function (event:MouseEvent):void
          {
            var target:Sprite = event.target as Sprite;
            //target.graphics.clear();
            var param_array:Array = target.name.split(";");
            var temp_container:Array = EntryArray_gray[parseInt(param_array[0])] as Array;
            var tempEntry:ColorEntry = temp_container[parseInt(param_array[1])] as ColorEntry;
            var temp_chip_num:int = tempEntry.getChipNum();
            var temp_dim:int = find_dim_from_count(tempEntry.getCount());

            /*
            var colorrgb:ColorRGB = ColorArray[temp_chip_num];
            var color:uint = (colorrgb.getRed()<<16) + (colorrgb.getGreen()<<8) + colorrgb.getBlue();
            */
            var newhighlight:Sprite = new Sprite;
            newhighlight.x = target.x - 3;
            newhighlight.y = target.y - 3;
            newhighlight.graphics.lineStyle(2, highlightColor, 0.7);
            newhighlight.graphics.beginFill(0x000000, 0);
            newhighlight.graphics.drawRect(0, 0, temp_dim + 6, temp_dim + 6);
            newhighlight.graphics.endFill();
            highlightContainer.addChild(newhighlight);
            
            // NOW GO THROUGH ALL THE NEIGHBORS AND HIGHLIGHT THOSE TOO!
            // better yet, don't highlight them, just draw squares in a highlight array
            for(var k:int = 0; k < tempEntry.getNumNeighbors(); k++){
              var tempNeighbor:Neighbor = tempEntry.getNeighborAt(k);
              var cont_num:int = find_container_number(tempNeighbor.getColor());
              var index:int = tempNeighbor.getIndex();
              var tmp_container:Sprite = spriteContainer_gray.getChildAt(cont_num) as Sprite;
              var neighbor_sprite:Sprite = tmp_container.getChildAt(index) as Sprite;
              
              var param_array2:Array = neighbor_sprite.name.split(";");
              var temp_container2:Array = EntryArray_gray[parseInt(param_array2[0])] as Array;
              var tempEntry2:ColorEntry = temp_container2[parseInt(param_array2[1])] as ColorEntry;
              var temp_chip_num2:int = tempEntry2.getChipNum();
              var temp_dim2:int = find_dim_from_count(tempEntry2.getCount());
              
              var newhighlight2:Sprite = new Sprite;
              newhighlight2.x = neighbor_sprite.x - 3;
              newhighlight2.y = neighbor_sprite.y - 3;
              newhighlight2.graphics.lineStyle(2, highlightColor, 0.7);
              newhighlight2.graphics.beginFill(0x000000, 0);
              newhighlight2.graphics.drawRect(0, 0, temp_dim2 + 6, temp_dim2 + 6);
              newhighlight2.graphics.endFill();
              highlightContainer.addChild(newhighlight2);
            }
          });
          thisSprite.addEventListener(MouseEvent.MOUSE_OUT,
          function (event:MouseEvent):void
          {
            var target:Sprite = event.target as Sprite;
            while(highlightContainer.numChildren > 0)
              highlightContainer.removeChildAt(0);

          });
          
          cur_x += dim+5;
          if(cur_x > max_x){
            cur_x = ox;
            oy += 35;
          }
          thisContainerSprite.addChild(thisSprite);
        }
        spriteContainer_gray.addChild(thisContainerSprite);
      }
    }
    
    private function plotWhiteContainer():void{
      // step 1: plot the white container
      var ox:int = 10;
      var max_x:int = 1440;
      for(var i:int = 0; i < EntryArray_white.length; i++){
        var cur_x:int = ox;
        var container:Array = EntryArray_white[i] as Array;
        var oy:int = find_oy_from_container_index(i);
        var thisContainerSprite:Sprite = new Sprite();
        
        for(var j:int = 0; j < container.length; j++){
          var thisEntry:ColorEntry = container[j] as ColorEntry;
          var dim:int = find_dim_from_count(thisEntry.getCount());
          var chip_num:int = thisEntry.getChipNum();
          var colorrgb:ColorRGB = ColorArray[chip_num];
          var color:uint = (colorrgb.getRed()<<16) + (colorrgb.getGreen()<<8) + colorrgb.getBlue();

          var thisSprite:Sprite = new Sprite();
          thisSprite.x = cur_x;
          thisSprite.y = oy;
          thisSprite.name = i.toString() + ";" + j.toString();    // store the container index and the position index for event listeners
          
          thisSprite.graphics.beginFill(color);
          thisSprite.graphics.drawRect(0, 0, dim, dim);
          thisSprite.graphics.endFill();

          thisSprite.addEventListener(MouseEvent.MOUSE_OVER,
          function (event:MouseEvent):void
          {
            var target:Sprite = event.target as Sprite;
            //target.graphics.clear();
            var param_array:Array = target.name.split(";");
            var temp_container:Array = EntryArray_white[parseInt(param_array[0])] as Array;
            var tempEntry:ColorEntry = temp_container[parseInt(param_array[1])] as ColorEntry;
            var temp_chip_num:int = tempEntry.getChipNum();
            var temp_dim:int = find_dim_from_count(tempEntry.getCount());

            /*
            var colorrgb:ColorRGB = ColorArray[temp_chip_num];
            var color:uint = (colorrgb.getRed()<<16) + (colorrgb.getGreen()<<8) + colorrgb.getBlue();
            */
            var newhighlight:Sprite = new Sprite;
            newhighlight.x = target.x - 3;
            newhighlight.y = target.y - 3;
            newhighlight.graphics.lineStyle(2, highlightColor, 0.7);
            newhighlight.graphics.beginFill(0x000000, 0);
            newhighlight.graphics.drawRect(0, 0, temp_dim + 6, temp_dim + 6);
            newhighlight.graphics.endFill();
            highlightContainer.addChild(newhighlight);
            
            // NOW GO THROUGH ALL THE NEIGHBORS AND HIGHLIGHT THOSE TOO!
            // better yet, don't highlight them, just draw squares in a highlight array
            for(var k:int = 0; k < tempEntry.getNumNeighbors(); k++){
              var tempNeighbor:Neighbor = tempEntry.getNeighborAt(k);
              var cont_num:int = find_container_number(tempNeighbor.getColor());
              var index:int = tempNeighbor.getIndex();
              var tmp_container:Sprite = spriteContainer_white.getChildAt(cont_num) as Sprite;
              var neighbor_sprite:Sprite = tmp_container.getChildAt(index) as Sprite;
              
              var param_array2:Array = neighbor_sprite.name.split(";");
              var temp_container2:Array = EntryArray_white[parseInt(param_array2[0])] as Array;
              var tempEntry2:ColorEntry = temp_container2[parseInt(param_array2[1])] as ColorEntry;
              var temp_chip_num2:int = tempEntry2.getChipNum();
              var temp_dim2:int = find_dim_from_count(tempEntry2.getCount());
              
              var newhighlight2:Sprite = new Sprite;
              newhighlight2.x = neighbor_sprite.x - 3;
              newhighlight2.y = neighbor_sprite.y - 3;
              newhighlight2.graphics.lineStyle(2, highlightColor, 0.7);
              newhighlight2.graphics.beginFill(0x000000, 0);
              newhighlight2.graphics.drawRect(0, 0, temp_dim2 + 6, temp_dim2 + 6);
              newhighlight2.graphics.endFill();
              highlightContainer.addChild(newhighlight2);
            }
          });
          thisSprite.addEventListener(MouseEvent.MOUSE_OUT,
          function (event:MouseEvent):void
          {
            var target:Sprite = event.target as Sprite;
            while(highlightContainer.numChildren > 0)
              highlightContainer.removeChildAt(0);

          });
          
          cur_x += dim+5;
          if(cur_x > max_x){
            cur_x = ox;
            oy += 35;
          }
          thisContainerSprite.addChild(thisSprite);
        }
        spriteContainer_white.addChild(thisContainerSprite);
      }
    }
    
    private function find_dim_from_count(count:int):int{
      if(count > 10)
        return 30;
      else
        return count * 3;
    }
    
    private function find_oy_from_container_index(index:int):int{
      var green_oy:int = 40;
      var blue_oy:int = 160;
      var purple_oy:int = 280;
      var pink_oy:int = 400;
      var brown_oy:int = 520;
      var yellow_oy:int = 605;
      var orange_oy:int = 690;
      var red_oy:int = 740;
      var gray_oy:int = 790;
      var black_oy:int = 840;
      var white_oy:int = 890;
      
      if(index == 0) // white
        return white_oy;
      else if(index == 1) // black
        return black_oy;
      else if(index == 2) // gray
        return gray_oy;
      else if(index == 3)
        return red_oy;
      else if(index == 4)
        return green_oy;
      else if(index == 5)
        return yellow_oy;
      else if(index == 6)
        return brown_oy;
      else if(index == 7)
        return blue_oy;
      else if(index == 8)
        return purple_oy;
      else if(index == 9)
        return pink_oy;
      else
        return orange_oy;
    }
    
    private function is_number(c:String):Boolean{
      if(c == "0" || c == "1" || c == "2" || c == "3" || c == "4" || c == "5" || c == "6" || c == "7" || c == "8" || c == "9")
        return true;
        
      return false;
    }
    
    private function find_container_number(color:String):int{
      if(color == "white")
        return 0;
      else if(color == "black")
        return 1;
      else if(color == "gray")
        return 2;
      else if(color == "red")
        return 3;
      else if(color == "green")
        return 4;
      else if(color == "yellow")
        return 5;
      else if(color == "brown")
        return 6;
      else if(color == "blue")
        return 7;
      else if(color == "purple")
        return 8;
      else if(color == "pink")
        return 9;
      else if(color == "orange")
        return 10;
        
      return -1;
    }
    
    protected function bgDisplayChangeHandler(event:Event):void{
      
      var color:uint = 0xc0c0c0;  // default
      
      while(highlightContainer.numChildren > 0)
        highlightContainer.removeChildAt(0);
      
      if(myComboBoxes.getBGDisplay() == true){
        if(myComboBoxes.getBGSelectColor() == "black"){
          color = color_black;
          highlightColor = 0xffffff;
        }
        else if(myComboBoxes.getBGSelectColor() == "gray"){
          color = color_gray;
          highlightColor = 0xffffff;
        }
        else if(myComboBoxes.getBGSelectColor() == "white"){
          color = color_white;
          highlightColor = 0x000000;
        }
      

        myFrame.drawBoundingBoxes(color);
        myFrame.drawBackground(color);
      }
      else{
        myFrame.drawFrame();
      }

    }
    
    protected function bgSelectChangeHandler(event:Event):void{
      if(myComboBoxes.getBGSelectColor() == "black"){
          highlightColor = 0xffffff;
          spriteContainer_black.visible = true;
          spriteContainer_gray.visible = false;
          spriteContainer_white.visible = false;
        }
        else if(myComboBoxes.getBGSelectColor() == "gray"){
          highlightColor = 0xffffff;
          spriteContainer_black.visible = false;
          spriteContainer_gray.visible = true;
          spriteContainer_white.visible = false;
        }
        else if(myComboBoxes.getBGSelectColor() == "white"){
          highlightColor = 0x000000;
          spriteContainer_black.visible = false;
          spriteContainer_gray.visible = false;
          spriteContainer_white.visible = true;
        }
    }
    
    
    
    
    
    
    
  }
}
